import 'package:sqflite/sqflite.dart';

import '../dao/ImGroupDao.dart';
import '../dao/ImUserDao.dart';
import '../pojo/ImPojo.dart';
import 'DbUtil.dart';

class ImCacheFactory {

  static ImCacheFactory _instance = new ImCacheFactory();

  final _userMap = new Map<String, ImUser>();
  final _groupMap = new Map<int, ImGroup>();
  final _groupCodeMap = new Map<String, ImGroup>();
  final _hotMessageTotalMap = new Map<int, int>(); // 热点数据

  static ImCacheFactory getInstance() {
    return _instance;
  }

  // Future<ImUser?> getImUser(Database database, String userId) async {
  //   if (_userMap.containsKey(userId)) return Future.value(_userMap[userId]);
  //   var imUserDao = ImUserDao(database);
  //   var user = await imUserDao.getByUserId(userId);
  //   if (user != null) {
  //     _userMap[userId] = user;
  //     return user;
  //   }
  //
  //   throw new Exception(userId + "用户不存在");
  // }

  Future<ImUser?> getImUser(String userId) async {
    if (_userMap.containsKey(userId)) return Future.value(_userMap[userId]);
    var imUserDao = ImUserDao();
    var user = await imUserDao.getByUserId(userId);
    if (user != null) {
      _userMap[userId] = user;
      return user;
    }

    throw new Exception(userId + "用户不存在");
    // return null;
  }

  // Future<ImGroup?> getImGroup(Database database, String currentUserId, String groupCode) async {
  //   var imUser = await getImUser(database, currentUserId);
  //   var groupKey = '${imUser?.id}_${groupCode}';
  //   if (_groupCodeMap.containsKey(groupKey)) return Future.value(_groupCodeMap[groupKey]);
  //
  //   var imGroupDao = ImGroupDao(database);
  //   try {
  //     var group = await imGroupDao.getByCodeAndUserId(groupCode, currentUserId);
  //     if (group != null) {
  //       _groupCodeMap[groupKey] = group;
  //       var gid = group.id;
  //       if (gid != null) {
  //         _groupMap[gid] = group;
  //       }
  //       return group;
  //     }
  //   } finally {
  //     DbUtil.close(database);
  //   }
  //
  //   return null;
  // }

  Future<ImGroup?> getImGroup(String currentUserId, String groupCode) async {
    var imUser = await getImUser(currentUserId);
    var groupKey = '${imUser?.id}_${groupCode}';
    if (_groupCodeMap.containsKey(groupKey)) return Future.value(_groupCodeMap[groupKey]);

    var imGroupDao = ImGroupDao();
    var group = await imGroupDao.getByCodeAndUserId(groupCode, currentUserId);
    if (group != null) {
      await imGroupDao.loadGroupUsers(group);

      _groupCodeMap[groupKey] = group;
      var gid = group.id;
      if (gid != null) {
        _groupMap[gid] = group;
      }
      return group;
    }

    return null;
  }

  void incHotMessage(int sessionId, int value) {
    if (value < 1) return;
    var total = getHotMessageTotal(sessionId);
    total = total == null ? 0  : total;
    total += value;
    _hotMessageTotalMap[sessionId] = total;
  }

  int getHotMessageTotal(int sessionId) {
    var total = _hotMessageTotalMap[sessionId];
    return total == null ? 0 : total;

  }

  void cleanHotMessage(int sessionId) {
    _hotMessageTotalMap.remove(sessionId);
  }
}